<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="filtersFormGroup" (ngSubmit)="save()" style="width: 700px;">
  <mat-toolbar color="primary">
    <h2>{{ title | translate }}</h2>
    <span class="flex-1"></span>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div style="height: 4px;" *ngIf="(isLoading$ | async) === false"></div>
  <div mat-dialog-content>
    <div class="tb-filters-header flex flex-row items-center justify-start">
      <span class="max-w-5% flex-full"></span>
      <div class="flex max-w-95% flex-full flex-row items-center justify-start">
        <div class="tb-header-label flex-1" translate>filter.filter</div>
        <div class="tb-header-label flex min-w-20 max-w-20 flex-1 basis-20 flex-col items-center justify-center" translate
             style="padding-left: 5px;">filter.editable</div>
        <div style="min-width: 132px;"></div>
      </div>
    </div>
    <fieldset [disabled]="isLoading$ | async">
      <mat-divider></mat-divider>
      <div class="flex flex-1 flex-row items-center justify-start"
           formArrayName="filters"
           *ngFor="let filterControl of filtersFormArray().controls; let $index = index">
        <span class="max-w-5% flex-full">{{$index + 1}}.</span>
        <div class="mat-elevation-z4 tb-filter flex max-w-95% flex-full flex-row items-center justify-start">
          <mat-label class="flex-1">{{filterControl.get('filter').value}}</mat-label>
          <section style="padding-left: 10px;"
                   class="tb-editable-switch flex min-w-20 max-w-20 flex-1 basis-20 flex-col items-center justify-center">
            <mat-slide-toggle class="editable-switch"
                              [formControl]="filterControl.get('editable')">
            </mat-slide-toggle>
          </section>
          <button [disabled]="isLoading$ | async"
                  mat-icon-button color="primary"
                  style="min-width: 40px;"
                  type="button"
                  (click)="editFilter($index)"
                  matTooltip="{{ 'filter.edit' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>edit</mat-icon>
          </button>
          <button [disabled]="isLoading$ | async"
                  mat-icon-button color="primary"
                  style="min-width: 48px;"
                  type="button"
                  (click)="duplicateFilter($index)"
                  matTooltip="{{ 'filter.duplicate-filter-action' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>content_copy</mat-icon>
          </button>
          <button [disabled]="isLoading$ | async"
                  mat-icon-button color="primary"
                  style="min-width: 40px;"
                  type="button"
                  (click)="removeFilter($index)"
                  matTooltip="{{ 'filter.remove-filter' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>close</mat-icon>
          </button>
        </div>
      </div>
    </fieldset>
  </div>
  <div mat-dialog-actions>
    <button mat-raised-button color="primary"
            type="button"
            (click)="addFilter()"
            [class.!hidden]="disableAdd"
            [disabled]="isLoading$ | async"
            matTooltip="{{ 'filter.add' | translate }}"
            matTooltipPosition="above">
      {{ 'filter.add' | translate }}
    </button>
    <span class="flex-1"></span>
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            (click)="cancel()"
            cdkFocusInitial>
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            type="submit"
            [disabled]="(isLoading$ | async) || filtersFormGroup.invalid || !filtersFormGroup.dirty">
      {{ 'action.save' | translate }}
    </button>
  </div>
</form>
